<?xml version="1.0"?>
<!DOCTYPE stylesheet [
        <!ENTITY t2 "tag:tupeloproject.org,2006:/2.0/">
        <!ENTITY dc "http://purl.org/dc/elements/1.1/">
        <!ENTITY ds "http://cet.ncsa.uiuc.edu/2008/2/20/Data.owl#">
        <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
        <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#">
        <!ENTITY time "http://www.w3.org/2006/time#">
        <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#">
        <!ENTITY geo "http://www.georss.org/georss/">
        <!ENTITY wgs84 "http://www.w3.org/2003/01/geo/wgs84_pos#">
        <!ENTITY kml "http://earth.google.com/kml/2.1">
        <!ENTITY gml "http://www.opengis.net/gml/">
        <!ENTITY nws_pfx "http://www.srh.noaa.gov/data/radar/poly.kml/">
        <!ENTITY nws "http://www.srh.noaa.gov/data/radar/poly.kml/ns#">
        <!ENTITY cet "http://cet.ncsa.uiuc.edu/2007/">
        <!ENTITY dcterms "http://purl.org/dc/terms/">
        ]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:k="&kml;"
                xmlns:ds="&ds;" xmlns:rdf="&rdf;" xmlns:rdfs="&rdfs;" xmlns:time="&time;"
                xmlns:xsd="&xsd;" xmlns:geo="&geo;" xmlns:nws="&nws;" xmlns:wgs84="&wgs84;"
                xmlns:dc="&dc;" xmlns:gml="&gml;" xmlns:cet="&cet;" xmlns:dcterms="&dcterms;">

    <!-- no output except what we generate -->
    <xsl:template match="text()|@*"/>
    <!-- and make it pretty -->
    <xsl:output indent="yes"/>

    <!-- top level -->
    <xsl:template match="/k:kml/k:Folder">
        <xsl:variable name="time-accessed">
            <xsl:call-template name="parse-file-timestamp">
                <xsl:with-param name="timestamp">
                    <xsl:value-of select="substring-before(substring-after(k:name,'['),']')"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:variable>
        <rdf:RDF>
            <ds:DataStreamToken>
                <xsl:attribute namespace="&rdf;" name="about">
                    <xsl:text>&nws_pfx;accessed#</xsl:text>
                    <xsl:value-of select="$time-accessed"/>
                </xsl:attribute>
                <ds:hasTimeOfOccurrence>
                    <time:Instant>
                        <time:inXSDDateTime rdf:datatype="&xsd;DateTime">
                            <xsl:value-of select="$time-accessed"/>
                        </time:inXSDDateTime>
                    </time:Instant>
                </ds:hasTimeOfOccurrence>
                <!-- find the placemarks that represent warnings -->
                <xsl:apply-templates select="k:Placemark[k:description[k:pre]]"/>
            </ds:DataStreamToken>
        </rdf:RDF>
    </xsl:template>

    <!-- this is the placemark that contains the description with VTEC timestamps -->
    <xsl:template match="k:Placemark[k:description[k:pre]]">
        <cet:hasConstituent>
            <nws:Warning>
                <xsl:attribute namespace="&rdf;" name="about">
                    <xsl:variable name="vtec-timestamp">
                        <xsl:call-template name="extract-vtec-timestamp">
                            <xsl:with-param name="description" select="."/>
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:text>&nws_pfx;vtec</xsl:text>
                    <xsl:value-of select="substring-before($vtec-timestamp,'Z/')"/>
                    <xsl:text>Z</xsl:text>
                </xsl:attribute>
                <xsl:apply-templates select="k:description/k:pre"/>
                <!-- the next sibling contains the shape -->
                <dcterms:spatial>
                    <xsl:apply-templates
                            select="following-sibling::k:Placemark[1]/k:LineString/k:coordinates"/>
                </dcterms:spatial>
            </nws:Warning>
        </cet:hasConstituent>
    </xsl:template>

    <!-- this template is only invoked for the shape of the warning, not for the point data -->
    <xsl:template match="k:coordinates">
        <gml:Polygon>
            <gml:exterior>
                <gml:LinearRing>
                    <gml:posList>
                        <!-- split the coordinates into points -->
                        <xsl:variable name="point-data">
                            <xsl:call-template name="split-shape">
                                <xsl:with-param name="shape" select="."/>
                            </xsl:call-template>
                        </xsl:variable>
                        <!-- trim the leading space -->
                        <xsl:value-of select="substring-after($point-data,' ')"/>
                    </gml:posList>
                </gml:LinearRing>
            </gml:exterior>
        </gml:Polygon>
    </xsl:template>

    <!-- convert the file's timestamp into ISO 8601 -->
    <!-- it's in the format YYYY MM/DD HH:MM:SS ZZZ -->
    <xsl:template name="parse-file-timestamp">
        <xsl:param name="timestamp"/>
        <!-- YYYYMMDD -->
        <xsl:value-of select="substring($timestamp,1,4)"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="substring($timestamp,6,2)"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="substring($timestamp,9,2)"/>
        <xsl:text>T</xsl:text>
        <!-- HHMMSSZ -->
        <xsl:value-of select="substring($timestamp,12,8)"/>
        <xsl:text>Z</xsl:text>
    </xsl:template>

    <!-- pick out the primary VTEC timestamp from the description field -->
    <xsl:template name="extract-vtec-timestamp">
        <xsl:param name="description"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of
                select="substring-before(substring-after(normalize-space($description),'- /'),'Z/')"/>
        <xsl:text>Z/</xsl:text>
    </xsl:template>

    <!-- extract event metadata from the primary VTEC timestamp from a description -->
    <xsl:template match="k:pre">
        <!-- extract the timestamp -->
        <xsl:variable name="timestamp">
            <xsl:call-template name="extract-vtec-timestamp">
                <xsl:with-param name="description" select="."/>
            </xsl:call-template>
        </xsl:variable>
        <!-- now extract metadata -->
        <!-- FIXME make these URIs -->
        <nws:productClass>
            <xsl:attribute namespace="&rdf;" name="resource">
                <xsl:text>&nws_pfx;productClass#</xsl:text>
                <xsl:value-of select="substring($timestamp,2,1)"/>
            </xsl:attribute>
        </nws:productClass>
        <nws:action>
            <xsl:attribute namespace="&rdf;" name="resource">
                <xsl:text>&nws_pfx;action#</xsl:text>
                <xsl:value-of select="substring($timestamp,4,3)"/>
            </xsl:attribute>
        </nws:action>
        <nws:office>
            <xsl:attribute namespace="&rdf;" name="resource">
                <xsl:text>&nws_pfx;office#</xsl:text>
                <xsl:value-of select="substring($timestamp,8,4)"/>
            </xsl:attribute>
        </nws:office>
        <nws:phenomena>
            <xsl:attribute namespace="&rdf;" name="resource">
                <xsl:text>&nws_pfx;phenomena#</xsl:text>
                <xsl:value-of select="substring($timestamp,13,2)"/>
            </xsl:attribute>
        </nws:phenomena>
        <nws:significance>
            <xsl:attribute namespace="&rdf;" name="resource">
                <xsl:text>&nws_pfx;significance#</xsl:text>
                <xsl:value-of select="substring($timestamp,16,1)"/>
            </xsl:attribute>
        </nws:significance>
        <nws:trackingNumber>
            <xsl:attribute namespace="&rdf;" name="resource">
                <xsl:text>&nws_pfx;trackingNumber#</xsl:text>
                <xsl:value-of select="substring($timestamp,18,4)"/>
            </xsl:attribute>
        </nws:trackingNumber>
        <xsl:variable name="start-timestamp">
            <xsl:value-of select="substring($timestamp,23,12)"/>
        </xsl:variable>
        <xsl:variable name="end-timestamp">
            <xsl:value-of select="substring($timestamp,36,12)"/>
        </xsl:variable>
        <dcterms:temporal>
            <time:Interval>
                <!-- FIXME deal with open intervals (where the timestamp is 000000T000000Z) -->
                <xsl:if test="not($start-timestamp='000000T0000Z')">
                    <time:hasBeginning>
                        <time:Instant>
                            <time:inXSDDateTime rdf:datatype="&xsd;DateTime">
                                <xsl:call-template name="parse-vtec-timestamp">
                                    <xsl:with-param name="timestamp" select="$start-timestamp"/>
                                </xsl:call-template>
                            </time:inXSDDateTime>
                        </time:Instant>
                    </time:hasBeginning>
                </xsl:if>
                <time:hasEnd>
                    <time:Instant>
                        <time:inXSDDateTime rdf:datatype="&xsd;DateTime">
                            <xsl:call-template name="parse-vtec-timestamp">
                                <xsl:with-param name="timestamp" select="$end-timestamp"/>
                            </xsl:call-template>
                        </time:inXSDDateTime>
                    </time:Instant>
                </time:hasEnd>
            </time:Interval>
        </dcterms:temporal>
    </xsl:template>

    <!-- convert a VTEC timestamp to ISO 8601 -->
    <!-- it's in the format YYMMDDTHHMMZ (note the Y2.1K bug!) -->
    <xsl:template name="parse-vtec-timestamp">
        <xsl:param name="timestamp"/>
        <xsl:text>20</xsl:text>
        <!-- said Y2.1K bug -->
        <xsl:value-of select="substring($timestamp,1,2)"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="substring($timestamp,3,2)"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="substring($timestamp,5,5)"/>
        <xsl:text>:</xsl:text>
        <xsl:value-of select="substring($timestamp,10,2)"/>
        <xsl:text>:00Z</xsl:text>
    </xsl:template>

    <!-- recursive gunk to parse a shape -->
    <xsl:template name="split-shape">
        <xsl:param name="shape"/>
        <xsl:variable name="nlist" select="concat(normalize-space($shape),' ')"/>
        <xsl:variable name="first" select="substring-before($nlist,' ')"/>
        <xsl:variable name="rest" select="substring-after($nlist,' ')"/>
        <!-- generate the first point -->
        <xsl:call-template name="parse-coordinates">
            <xsl:with-param name="coordinates" select="$first"/>
        </xsl:call-template>
        <!-- now do the remaining points -->
        <xsl:if test="$rest">
            <xsl:call-template name="split-shape">
                <xsl:with-param name="shape" select="$rest"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <!-- parse a lon,lat,alt record -->
    <xsl:template name="parse-coordinates">
        <xsl:param name="coordinates"/>
        <xsl:variable name="lon" select="substring-before($coordinates,',')"/>
        <xsl:variable name="la" select="substring-after($coordinates,',')"/>
        <xsl:variable name="lat" select="substring-before($la,',')"/>
        <xsl:variable name="alt" select="substring-after($la,',')"/>
        <!-- [space] lat [space] lon -->
        <xsl:text></xsl:text>
        <xsl:value-of select="$lat"/>
        <xsl:text></xsl:text>
        <xsl:value-of select="$lon"/>
        <!-- discard the altitude for now; it's always 25 -->
        <!--alt>
            <xsl:value-of select="$alt"/>
        </alt-->
    </xsl:template>

</xsl:stylesheet>
